<?php
/**
 * 浏览最多业务类
 * @author 祝文博<kevinroot@163.com>
 * @create 2013年05月31日23:47:47
 */
class Business_MostViewed
{
    protected $_tableInfo;
	protected $_table;
	
	protected static $_instance;
	
	public static function getInstance()
	{
	    if (empty(self::$_instance)) {
	        self::$_instance = new self();
	    }
	    return self::$_instance;
	}
	
	public function __construct()
	{
		$this->_tableInfo = Zend_Registry::get('database')->mostviewed;
		$this->_table = Zend_Registry::get('database')->mostviewed->tablename;
	}
	
	/**
	 * 获得所有文章
	 * @return Array
	 */
	public function getAll()
	{
		$select = Utility_Db::getInstance()->conn()->select();
		$select->from($this->_table, "*");
		$stmt = $select->query();
		$result = $stmt->fetchAll();
		return $result;
	}
	
	/**
	 * 保存文章
	 * @param Array $data
	 * @return boolean
	 */
	public function save($data)
	{
		if (empty($data[$this->_tableInfo->id])) {
			unset($data[$this->_tableInfo->id]);
		}
	
		$data[$this->_tableInfo->ctime] = date("Y-m-d H:i:s");
		$data[$this->_tableInfo->mtime] = date("Y-m-d H:i:s");
	
		$db = Utility_Db::getInstance()->conn();
		
		$where = array(
			$this->_tableInfo->id . ' = ?' => $data[$this->_tableInfo->id]
		);
		unset($data[$this->_tableInfo->id]);
		unset($data[$this->_tableInfo->ctime]);
		$affectRows = $db->update($this->_table, $data, $where);
		if (!$affectRows) {
			$data[$this->_tableInfo->ctime] = date("Y-m-d H:i:s");
			$insRowNum = $db->insert($this->_table, $data);
		}
		if (!isset($insRowNum) && !isset($affectRows)) {
			return false;
		} else {
			return true;
		}
	}
}